package config;

import Bean.UserInfoBean;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBConfig {
    private static String url;
    private static String user;
    private static String password;

    static {
        try {
            // 读取配置文件
            Properties properties = new Properties();
            properties.load(new FileInputStream("db.properties"));

            // 读取配置信息
            url = properties.getProperty("db.url");
            user = properties.getProperty("db.user");
            password = properties.getProperty("db.password");

            // 加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

        } catch (IOException e) {
            System.out.println("数据库配置有误");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private DBConfig(){
        throw new UnsupportedOperationException("no");
    }
    // 获取数据库连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

}

